import { Window } from "../../../../component/window/Window";
import { GlobalClass } from "../../../../../com/mui/core/GlobalClass";
import { ImagesConst } from "../../../resource/ImagesConst";
import { ObjCreate } from "../../common/ObjCreate";
import { Lv70DramaMapTabItem } from "./Lv70DramaMapTabItem";
import { Global } from "../../../../../com/gengine/global/Global";
import { GCheckBox } from "../../../../../com/mui/controls/GCheckBox";
import { MsgManager } from "../../../manager/MsgManager";
import { Language } from "../../../../../extend/language/Language";
import { DramaMapConfig } from "../../../resource/DramaMapConfig";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { Lv70DramaDifficulty } from "../data/Lv70DramaDifficulty";
import { Lv70DramaConfig } from "../../../resource/Lv70DramaConfig";
import { Cache } from "../../../cache/Cache";
import { Alert } from "../../../../../com/mui/controls/Alert";
import { TDramaCopy } from "../../../../../Message/Db/Tables/TDramaCopy";
import { Lv70DramaEnterView } from "./Lv70DramaEnterView";
import { IDragDrop } from "../../../../../com/mui/manager/IDragDrop";
import { Lv70DramaSubMap } from "./Lv70DramaSubMap";
import { SystemSetter } from "../../system/SystemSetter";
import { DisplayUtil } from "../../../../common/DisplayUtil";
import { Log } from "../../../../../com/gengine/debug/Log";
import { Lv70DramaMapInfo } from "../data/Lv70DramaMapInfo";
import { GTextFiled } from "../../../../../com/mui/controls/GTextFiled";
import { GButton } from "../../../../../com/mui/controls/GButton";
type int = number;
//class Lv70DramaMapMain
    
    export  class Lv70DramaMapMain extends Window
    {
        constructor(arg1: int)
        {
            super();
            this._stagerect = new Object /* flash.geom.Rectangle */();
            this._enableChapterNum = arg1;
            this._nextTabTime = 0;
            this._nextDifficultyTime = 0;
            this._curMapId = 1;
            this._subMaps = [];
            this.initView();
            this.initDrag(this._dragSource);
            return;
        }

        /* internal  */initView(): void
        {
            var loc2=0;
            var loc4=null;
            var loc1=GlobalClass.getBitmap(ImagesConst.Lv70DramaMapBg);
            this.addChild(loc1);
            this._dragSource = new Object /* flash.display.Sprite */();
            this.addChild(this._dragSource);
            this._leftDrag = new Object /* flash.display.Sprite */();
            this._leftDrag.x = 12;
            this._leftDrag.y = 25;
            this._leftDrag.graphics.beginFill(16777215, 0);
            this._leftDrag.graphics.drawRect(0, 0, 60, 517);
            this._leftDrag.graphics.endFill();
            this._dragSource.addChild(this._leftDrag);
            this._rightDrag = new Object /* flash.display.Sprite */();
            this._rightDrag.x = 760;
            this._rightDrag.y = 25;
            this._rightDrag.graphics.beginFill(16777215, 0);
            this._rightDrag.graphics.drawRect(0, 0, 60, 517);
            this._rightDrag.graphics.endFill();
            this._dragSource.addChild(this._rightDrag);
            this._curMapContainer = new Object /* flash.display.Sprite */();
            this.addChild(this._curMapContainer);
            this._btnClose = ObjCreate.createGButton("", 789, 27, 27, 28, this, "RedBigXCloseBtn");
            this._btnClose.addEventListener(flash.events.MouseEvent.CLICK, this.clickCloseHandler);
            this._boxEasy = ObjCreate.createCheckBox("", 623, 64, 20, 20, this);
            this._boxBm1 = GlobalClass.getBitmap(ImagesConst.Lv70DramaMapJianDan);
            this._boxBm1.x = this._boxEasy.x + 21;
            this._boxBm1.y = this._boxEasy.y - 5;
            this.addChild(this._boxBm1);
            this._boxDifficult = ObjCreate.createCheckBox("", 623, 93, 20, 20, this);
            this._boxBm2 = GlobalClass.getBitmap(ImagesConst.Lv70DramaMapKunNan);
            this._boxBm2.x = this._boxDifficult.x + 21;
            this._boxBm2.y = this._boxDifficult.y - 5;
            this.addChild(this._boxBm2);
            this._tabs = [];
            loc2 = 1;
            while (loc2 <= this.ChapterNum) 
            {
                loc1 = GlobalClass.getBitmap(this.TabLayerPreName + loc2.toString());
                (loc4 = new Lv70DramaMapTabItem(loc2, loc1)).x = (loc2 - 1) * this.TabDistance + 78;
                loc4.y = -2;
                this.addChild(loc4);
                this._tabs[loc2] = loc4;
                if (loc2 > this._enableChapterNum) 
                {
                    loc4.enabled = false;
                }
                else 
                {
                    loc4.enabled = true;
                }
                loc4.addEventListener(flash.events.MouseEvent.CLICK, this.clickTabHandler);
                ++loc2;
            }
            this._boxEasy.addEventListener(flash.events.Event.CHANGE, this.boxSelectedChangedHandler);
            this._boxDifficult.addEventListener(flash.events.Event.CHANGE, this.boxSelectedChangedHandler);
            this._txtTimes = ObjCreate.createGTextField("", 590, 475, 250, 20, this);
            var loc3=this._txtTimes.defaultTextFormat;
            loc3.size = 16;
            this._txtTimes.defaultTextFormat = loc3;
            return;
        }

        /* internal  */setDifficultySelecteBoxPlaces(arg1: Object /* flash.geom.Point */): void
        {
            if (arg1 != null) 
            {
                this._boxEasy.x = arg1.x;
                this._boxEasy.y = arg1.y;
                this._boxDifficult.x = this._boxEasy.x;
                this._boxDifficult.y = this._boxEasy.y + 29;
            }
            this._boxBm1.x = this._boxEasy.x + 21;
            this._boxBm1.y = this._boxEasy.y - 5;
            this._boxBm2.x = this._boxDifficult.x + 21;
            this._boxBm2.y = this._boxDifficult.y - 5;
            return;
        }

        /* internal  */initDrag(arg1: Object /* flash.display.Sprite */): void
        {
            arg1.addEventListener(flash.events.MouseEvent.MOUSE_DOWN, this.beginDragHandler);
            return;
        }

        /* internal  */beginDragHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            Global.stage.addEventListener(flash.events.MouseEvent.MOUSE_UP, this.onMouseUpHandler);
            this._stagerect.x = 0;
            this._stagerect.y = 0;
            this._stagerect.width = this.stage.stageWidth;
            this._stagerect.height = this.stage.stageHeight;
            this.startDrag(false, this._stagerect);
            return;
        }

        /* internal  */onMouseUpHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            Global.stage.removeEventListener(flash.events.MouseEvent.MOUSE_UP, this.onMouseUpHandler);
            this.stopDrag();
            return;
        }

        /* internal  */boxSelectedChangedHandler(arg1: Object /* flash.events.Event */): void
        {
            var loc4=0;
            var loc1=arg1.target as GCheckBox;
            if (!loc1.selected && this._lastBox == loc1) 
            {
                loc1.selected = true;
                return;
            }
            var loc2=flash.utils.getTimer();
            if (loc2 < this._nextDifficultyTime) 
            {
                this._boxEasy.selected = false;
                this._boxDifficult.selected = false;
                this._lastBox.selected = true;
                loc4 = (loc4 = int(this._nextDifficultyTime - loc2)) / 1000 + 1;
                MsgManager.showRollTipsMsg(Language.getStringByParam(52066, loc4.toString()));
                return;
            }
            var loc3=DramaMapConfig.instance.getMapInfo(this._curMapId);
            if (loc1 == this._boxEasy && !(loc3 == null) && !loc3.haveEasy) 
            {
                MsgManager.showRollTipsMsg(Language.getStringByParam(80272, Language.getString(80273)));
                loc1.selected = false;
                return;
            }
            if (loc1 == this._boxDifficult && !(loc3 == null) && !loc3.haveDifficulty) 
            {
                MsgManager.showRollTipsMsg(Language.getStringByParam(80272, Language.getString(80274)));
                loc1.selected = false;
                return;
            }
            this._boxEasy.selected = false;
            this._boxDifficult.selected = false;
            loc1.selected = true;
            this._lastBox = loc1;
            this._nextDifficultyTime = loc2 + Lv70DramaMapMain.ChangeDifficultyTime;
            Dispatcher.dispatchEvent(new DataEvent(EventName.Lv70DramaMapDifficultyChanged, this.getDifficulty()));
            return;
        }

        public setDifficultySelected(arg1: int): void
        {
            if (arg1 != Lv70DramaDifficulty.Easy) 
            {
                if (arg1 == Lv70DramaDifficulty.Difficult) 
                {
                    this._boxEasy.selected = false;
                    this._boxDifficult.selected = true;
                }
            }
            else 
            {
                this._boxEasy.selected = true;
                this._boxDifficult.selected = false;
            }
            return;
        }

        /* internal  */clickTabHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc3=0;
            var loc1=flash.utils.getTimer();
            var loc2=arg1.currentTarget as Lv70DramaMapTabItem;
            if (loc2.selected) 
            {
                return;
            }
            if (loc1 < this._nextTabTime) 
            {
                loc3 = (loc3 = int(this._nextTabTime - loc1)) / 1000 + 1;
                MsgManager.showRollTipsMsg(Language.getStringByParam(52066, loc3.toString()));
                return;
            }
            this._nextTabTime = loc1 + Lv70DramaMapMain.ChangeTabTime;
            Dispatcher.dispatchEvent(new DataEvent(EventName.Lv70DramaMapTabChange, loc2.mapId));
            this.hideEnterView();
            return;
        }

        /* internal  */getCurMapData(): Array<any>
        {
            var loc4=null;
            var loc1=[];
            var loc2=this.getDifficulty();
            if (loc2 <= 0) 
            {
                loc2 = 1;
            }
            var loc3=1;
            while (loc3 <= 20) 
            {
                if ((loc4 = Lv70DramaConfig.instance.getDramaCopy(this._curMapId, loc2, loc3)) == null) 
                {
                    break;
                }
                loc1.push(loc4);
                ++loc3;
            }
            return loc1;
        }

        /* internal  */getCurMapGrayData(): Array<any>
        {
            var loc5=null;
            var loc6=null;
            var loc7=false;
            var loc1=[];
            var loc2=this.getDifficulty();
            if (loc2 <= 0) 
            {
                return [false, true, true, true, true, true];
            }
            var loc3=Cache.instance.copy.getLv70HightesHistory(loc2);
            if (loc3 == 0 && this._curMapId == 1) 
            {
                if ((loc5 = Lv70DramaConfig.instance.getDramaCopy(this._curMapId, loc2, 1)) != null) 
                {
                    loc3 = loc5.copyCode;
                }
            }
            else if (loc3 != 0) 
            {
                ++loc3;
            }
            var loc4=1;
            while (loc4 <= 20) 
            {
                if ((loc6 = Lv70DramaConfig.instance.getDramaCopy(this._curMapId, loc2, loc4)) == null) 
                {
                    break;
                }
                if (loc6.copyCode <= loc3) 
                {
                    loc1[(loc4 - 1)] = false;
                }
                else 
                {
                    loc1[(loc4 - 1)] = true;
                }
                if (loc7 = Cache.instance.copy.isLv70DramaTodayDone(loc6.copyCode)) 
                {
                    loc1[(loc4 - 1)] = true;
                }
                ++loc4;
            }
            return loc1;
        }

        /* internal  */enterDramaHandler(arg1: int): void
        {
            var loc6=0;
            if (this._curMap == null) 
            {
                return;
            }
            var loc1=this.getDifficulty();
            if (loc1 == -1) 
            {
                MsgManager.showRollTipsMsg(Language.getString(80142));
                return;
            }
            var loc2=this._curMap.mapId;
            var loc3;
            if ((loc3 = Lv70DramaConfig.instance.getDramaCopy(loc2, loc1, arg1)) == null) 
            {
                MsgManager.showRollTipsMsg(Language.getString(80122));
                return;
            }
            var loc4;
            if ((loc4 = Cache.instance.copy.getLv70HightesHistory(loc1)) < loc3.copyCode) 
            {
                if ((loc6 = parseInt(Cache.instance.role.combatCapabilities)) < loc3.requireCombat) 
                {
                    Alert.show(Language.getStringByParam(80134, "<font color=\'#ff0000\'>" + loc3.requireCombat.toString() + "</font>"), null, Alert.OK);
                    return;
                }
            }
            Lv70DramaConfig.instance.curCopy = loc3;
            var loc5=true;
            if (loc4 >= loc3.copyCode) 
            {
                loc5 = false;
            }
            if (loc5) 
            {
                this.enterDrama(loc3, loc5);
            }
            else 
            {
                this.showEnterView(loc3);
            }
            return;
        }

        /* internal  */enterDrama(arg1: TDramaCopy, arg2: Boolean): void
        {
            this.hide();
            Dispatcher.dispatchEvent(new DataEvent(EventName.Lv70DramaEnterDrama, {"config": arg1, "isDrama": arg2}));
            return;
        }

        /* internal  */showEnterView(arg1: TDramaCopy): void
        {
            if (!this._dramaEntrance) 
            {
                this._dramaEntrance = new Lv70DramaEnterView(this);
            }
            this._dramaEntrance.updateData(arg1, this.enterDrama, null);
            this._dramaEntrance.show((800 - this._dramaEntrance.width) / 2, (560 - this._dramaEntrance.height) / 2);
            this.checkWareHouseItems();
            return;
        }

        /* internal  */hideEnterView(): void
        {
            if (this._dramaEntrance) 
            {
                this._dramaEntrance.hide();
            }
            return;
        }

        public checkWareHouseItems(): void
        {
            if (this._dramaEntrance && !this._dramaEntrance.isHide) 
            {
                if (Cache.instance.pack.delegate.itemLength > 0) 
                {
                    this._dramaEntrance.highlightWareHouseBtn(true);
                }
                else 
                {
                    this._dramaEntrance.highlightWareHouseBtn(false);
                }
            }
            return;
        }

        /* internal  */getDifficulty(): int
        {
            if (this._boxEasy.selected) 
            {
                return 1;
            }
            if (this._boxDifficult.selected) 
            {
                return 2;
            }
            return -1;
        }

        /* internal  */clickCloseHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            this.hide();
            return;
        }

        public /* override */ get dragSource(): Object
        {
            return this._dragSource;
        }

        public /* override */ set dragSource(arg1: Object)
        {
            return;
        }

        public /* override */ get isDragAble(): Boolean
        {
            return true;
        }

        public /* override */ get isThrowAble(): Boolean
        {
            return false;
        }

        public /* override */ canDrop(arg1: IDragDrop, arg2: IDragDrop): Boolean
        {
            return false;
        }

        public /* override */ hide(): void
        {
            super.hide();
            this.hideEnterView();
            return;
        }

        protected /* override */ createDisposedChildrenImpl(): void
        {
            super.createDisposedChildrenImpl();
            if (closeBtn) 
            {
                closeBtn.visible = false;
                closeBtn.mouseEnabled = false;
            }
            return;
        }

        public /* override */ dispose(arg1: Boolean=true): void
        {
            var loc2=null;
            super.dispose();
            this._boxBm1 = null;
            this._boxBm2 = null;
            var loc1=0;
            while (!(this._subMaps == null) && loc1 < this._subMaps.length) 
            {
                loc2 = this._subMaps[loc1] as Lv70DramaSubMap;
                if (loc2 != null) 
                {
                    loc2.dispose();
                }
                ++loc1;
            }
            this._subMaps = null;
            if (this._dramaEntrance) 
            {
                this._dramaEntrance.hide();
                this._dramaEntrance.clean();
                this._dramaEntrance = null;
            }
            return;
        }

        public /* override */ show(arg1: int=0, arg2: int=0): void
        {
            super.show();
            this.x = (Global.stage.stageWidth - 800) / 2;
            this.y = (Global.stage.stageHeight - 560) / 2 + 40;
            return;
        }

        public updateTodayDoneTimes(arg1: int): void
        {
            if (arg1 < 0) 
            {
                return;
            }
            this._txtTimes.htmlText = Language.getStringByParam(80197, arg1.toString());
            return;
        }

        public changeTab(arg1: int): void
        {
            var loc2=null;
            var loc1=1;
            while (loc1 < this._tabs.length) 
            {
                loc2 = this._tabs[loc1] as Lv70DramaMapTabItem;
                if (loc2 != null) 
                {
                    if (loc1 != arg1) 
                    {
                        loc2.selected = false;
                    }
                    else 
                    {
                        loc2.selected = true;
                    }
                }
                ++loc1;
            }
            return;
        }

        public changeToMap(arg1: int): void
        {
            if (arg1 > this.ChapterNum || arg1 <= 0) 
            {
                return;
            }
            this._curMapId = arg1;
            var loc1=DramaMapConfig.instance.getMapInfo(this._curMapId);
            if (loc1 == null) 
            {
                return;
            }
            this._lastBox = null;
            this.setDifficultySelecteBoxPlaces(loc1.chooseBoxPlace);
            this.setDifficultySelectedByRecord(SystemSetter.localSetter.lv70DramaDifficulty);
            this.setDifficultySelectedByConfig(loc1);
            this.updateMap();
            return;
        }

        public updateMap(): void
        {
            if (this._curMap != null) 
            {
                DisplayUtil.removeMe(this._curMap);
            }
            this._curMap = this._subMaps[this._curMapId] as Lv70DramaSubMap;
            var loc1=DramaMapConfig.instance.getMapInfo(this._curMapId);
            if (loc1 == null) 
            {
                Log.debug("dramaMap.xml error: can\'t find mapInfo by mapId:" + this._curMapId.toString());
            }
            if (this._curMap == null && !(loc1 == null)) 
            {
                this._curMap = new Lv70DramaSubMap(loc1, this.enterDramaHandler);
                this._subMaps[this._curMapId] = this._curMap;
            }
            this._curMap.x = 86 + 200;
            this._curMap.y = 71 + 200;
            this._curMapContainer.addChild(this._curMap);
            this._curMap.updateMap(this.getCurMapGrayData(), this.getCurMapData(), this.getDifficulty());
            return;
        }

        public /* override */ get isDropAble(): Boolean
        {
            return false;
        }

        /* internal  */setDifficultySelectedByRecord(arg1: int): void
        {
            if (this._boxEasy.selected || this._boxDifficult.selected) 
            {
                return;
            }
            if (arg1 != Lv70DramaMapMain.Difficulty_Normal) 
            {
                if (arg1 == Lv70DramaMapMain.Difficulty_Difficul) 
                {
                    this._boxEasy.selected = false;
                    this._boxDifficult.selected = true;
                }
            }
            else 
            {
                this._boxEasy.selected = true;
                this._boxDifficult.selected = false;
            }
            return;
        }

        /* internal  */setDifficultySelectedByConfig(arg1: Lv70DramaMapInfo): void
        {
            if (arg1 == null) 
            {
                return;
            }
            if (!arg1.haveEasy && this._boxEasy.selected) 
            {
                this._boxEasy.selected = false;
            }
            if (!arg1.haveDifficulty && this._boxDifficult.selected) 
            {
                this._boxDifficult.selected = false;
            }
            if (arg1.haveEasy && !arg1.haveDifficulty) 
            {
                this._boxEasy.selected = true;
            }
            if (arg1.haveDifficulty && !arg1.haveEasy) 
            {
                this._boxDifficult.selected = true;
            }
            return;
        }

        /* internal */ /* const */TabDistance: int=114;

        /* internal */ /* const */TabLayerPreName: string="Lv70DramaMapLayer";

        /* internal */ /* const */ChapterNum: int=6;

        public /* const */MaxDoneTime: int=3;

        public static /* const */Difficulty_Normal: int=1;

        public static /* const */Difficulty_Difficul: int=2;

        public static /* const */ChangeTabTime: int=1000;

        public static /* const */ChangeDifficultyTime: int=1000;

        /* internal */ /* var */_dramaEntrance: Lv70DramaEnterView;

        /* internal */ /* var */_leftDrag: Object /* flash.display.Sprite */;

        /* internal */ /* var */_rightDrag: Object /* flash.display.Sprite */;

        /* internal */ /* var */_dragSource: Object /* flash.display.Sprite */;

        /* internal */ /* var */_txtTimes: GTextFiled;

        /* internal */ /* var */_btnClose: GButton;

        /* internal */ /* var */_subMaps: Array<any>;

        /* internal */ /* var */_curMapId: int;

        /* internal */ /* var */_curMap: Lv70DramaSubMap;

        /* internal */ /* var */_boxEasy: GCheckBox;

        /* internal */ /* var */_boxBm1: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_boxDifficult: GCheckBox;

        /* internal */ /* var */_boxBm2: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_curMapContainer: Object /* flash.display.Sprite */;

        /* internal */ /* var */_nextTabTime: number;

        /* internal */ /* var */_nextDifficultyTime: number;

        /* internal */ /* var */_lastBox: GCheckBox;

        /* internal */ /* var */_enableChapterNum: int;

        /* internal */ /* var */_tabs: Array<any>;

        /* internal */ /* var */_stagerect: Object /* flash.geom.Rectangle */;
    }
